package com.she.iiicoursecenter.view.tab4;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab4_message.Tab4_MainActivity;
import com.she.iiicoursecenter.activity.tab4_message.Tab4_MessageDetail;
import com.she.iiicoursecenter.adapter.Tab4_Adapter;
import com.she.iiicoursecenter.bean.MessageDetailBundleBean;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.connect.bean.GetMsgBean;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab4_MainView extends CustomizedView implements OnClickListener {

	private ListView mListView;
	private Tab4_Adapter mAdapter;
	private List<GetMsgBean> data;

	public Tab4_MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		//
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab4_main, this, true);
		//
		
		//消除未讀訊息提醒
		Tab4_MainView.this.getApp().getMainActivityGroup().setMessageUnreadCount(0, false);
//		if (KenEventManager.getInstance().getMsgTime() < getApp().getMsgData().get(0).getTime()) {
//			KenEventManager.getInstance().setMsgTime(getApp().getMsgData().get(0).getTime());
//		}
		//
		data = getApp().getMsgData();
//		for (int i = 0; i < data.size(); i++) {
//			if ((i+1) <= getApp().msgCount) {
//				data.get(i).setIsNew(true);
//			}
//		}
		mAdapter = new Tab4_Adapter(getContext(), data);
		mListView = (ListView) this.findViewById(R.id.tab4_listview);
		mListView.setDivider(new ColorDrawable(Color.TRANSPARENT));// 去除listview分隔線
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				Message msg = Tab4_MainView.this.getApp().mChangeViewHandler.obtainMessage();
				msg.what = Tab4_MainView.this.getApp().MSG_WHAT_CHANGEVIEW;
				MessageDetailBundleBean bundleBean = new MessageDetailBundleBean(arg2);
				ViewChageDataBean dataBean = new ViewChageDataBean(Tab4_MainActivity.class, Tab4_MessageDetail.class, bundleBean);
				msg.obj = dataBean;
				Tab4_MainView.this.getApp().mChangeViewHandler.sendMessage(msg);
				data.get(arg2).setIsNew(false);
				KenEventManager.getInstance().setMsgIdRead(data.get(arg2).getMsgId() , data.get(arg2).getTime());
				getApp().fadein = true;
			}
		});

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void notifyAdapter(){
		mAdapter.notifyDataSetChanged();
		if (getApp().getMsgData().size() > 0 ) {
			if (KenEventManager.getInstance().getMsgTime() < getApp().getMsgData().get(0).getTime()) {
				KenEventManager.getInstance().setMsgTime(getApp().getMsgData().get(0).getTime());
			}
		}
	}
}
